<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="doAdd(1,'0')">添加权限</el-button>
        </div>
        <!-- row-key="id"让表格支持树状结构 -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="doAdd(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="doEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="doDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹窗结构 -->
    <el-dialog :visible.sync="showDialog" :title="isEdit? '编辑':'添加'" @close="doClose">
      <!-- 表单内容 -->
      <el-form ref="formDataRef" label-width="100px" :model="formData" :rules="formDataRules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="doSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, updatePermission, delPermission, getPermissionDetail } from '@/api/permissions'
import { arrayToTree } from '@/utils/arrayToTree'
export default {
  data() {
    return {
      // 权限列表信息
      list: [],
      // 是否显示弹窗
      showDialog: false,
      // 表单信息
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      // 判断是否是编辑
      isEdit: false,
      //
      formDataRules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ], // 名称
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }], // 权限标识
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }] // 描述
      }
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      const { data: res } = await getPermissionList()
      // console.log('权限列表', res)
      this.list = arrayToTree(res.data, '0')
    },
    doAdd(type, pid) {
      this.showDialog = true
      // 根据type判断是页面级还是按钮级
      this.formData.type = type
      this.formData.pid = pid
      this.isEdit = false
    },
    // 提交表单信息
    async doSubmit() {
      this.$refs.formDataRef.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            const { data: res } = await updatePermission(this.formData)
            // 提示信息
            this.$message.success(res.message)
            // 关闭弹窗
            this.showDialog = false
            // 刷新页面
            this.loadPermissionList()
          } else {
            const { data: res } = await addPermission(this.formData)
            console.log('添加权限', res)
            // 关闭弹窗
            this.showDialog = false
            // 刷新列表
            this.loadPermissionList()
          }
        }
      })
    },
    // 表单清空
    doClose() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: ''
      }
      this.$refs.formDataRef.resetFields()
    },
    // 删除
    doDel(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { data: res } = await delPermission(id)
        console.log('删除', res)
        // 提示信息
        this.$message.success(res.message)
        // 刷新页面
        this.loadPermissionList()
      }).catch(() => {
      })
    },
    // 点击编辑获取详情
    async doEdit(id) {
      this.isEdit = true
      this.showDialog = true
      const { data: res } = await getPermissionDetail(id)
      console.log('详情', res)
      this.formData = { ...res.data }
    }

  }

}
</script>

<style>

</style>
